package com.alks.function.controller.finance.statement;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.WebLogger;
import com.alks.entity.data.page.PageParam;
import com.alks.entity.data.page.PageRecord;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.deserializer.LocalDateTimeDeserialize;
import com.alks.function.service.finance.statement.GenerateProdReportService;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@Slf4j
@Api(value = "财务部", tags = {"财务部-报表分析"})
@ApiRestController("/GenerateProdReport")
//@RoleCheck(prgIds = {"100504","180504"})
public class GenerateProdReportController {

    @Autowired
    private GenerateProdReportService generateProdReportService;

    @GetMapping("/getMonthProd")
    @ApiOperation(value = "查询月产量",tags = "财务部-报表分析")
    public ResponseInfo getMonthProd(@RequestParam String year) {
        return generateProdReportService.getMonthProd(year);
    }

    @WebLogger
    @GetMapping("/exportMonthProd")
    @ApiOperation(value = "导出",tags = "财务部-报表分析")
    public void exportReconciliationStatistics(@ApiParam(value = "年份",required = true)  @RequestParam String year, HttpServletResponse response) throws IOException {
        generateProdReportService.exportMonthProd(year,response);
    }

    @PostMapping("/getDaily/report")
    @ApiOperation(value = "日产量统计",tags = "财务部-报表分析")
    public ResponseInfo getDailyProd(@RequestBody PageParam request,
                                     @RequestParam@ApiParam(value ="起始日期" ,required = true)@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
                                     @RequestParam@ApiParam(value ="结束日期" ,required = true)@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        /*分页初始化*/
        int pageSize = request.getPageSize() == null || request.getPageSize() == -1 ?10: request.getPageSize();
        int pageNum = request.getPageNum() == null || request.getPageNum() == -1 ?1:request.getPageNum();
        request.setPageSize(pageSize);
        request.setPageNum(pageNum);
        PageRecord<List<String>> map =  generateProdReportService.getDailyProd(request,startDate.atStartOfDay(),endDate.atStartOfDay());
        return ResponseInfo.ok(map);
    }

    @PostMapping("/getDaily/report/detail")
    @ApiOperation(value = "日产量报表明细",tags = "财务部-报表分析")
    public ResponseInfo getDailyProdDetail(@RequestParam String prodStep,
                                           @RequestParam String deptName,
                                           @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
        Map<String,Object> map =  generateProdReportService.getDailyProdDetail(prodStep,deptName,date.atStartOfDay());
        return ResponseInfo.ok(map);
    }

    @WebLogger
    @PostMapping("/getDaily/export")
    @ApiOperation(value = "日报表导出",tags = "财务部-报表分析")
    public void exportReconciliationStatistics(@RequestBody PageParam request,
                                               @RequestParam@ApiParam(value ="起始日期" ,required = true)@JsonDeserialize(using = LocalDateTimeDeserialize.class) LocalDateTime startDate,
                                               @RequestParam@ApiParam(value ="结束日期" ,required = true)@JsonDeserialize(using = LocalDateTimeDeserialize.class) LocalDateTime endDate,
                                               HttpServletResponse response) throws IOException {
    }

    @WebLogger
    @GetMapping("/dept/update")
    @ApiOperation(value = "部门列表更新",tags = "财务部-报表分析")
    public ResponseInfo deptListUpdate() {
        generateProdReportService.deptListUpdate();
        return ResponseInfo.ok();
    }

    @GetMapping("/updateMonthProd")
    @ApiOperation(value = "更新月产量",tags = "财务部-报表分析")
    public ResponseInfo updateMonthProd(@RequestParam String year,@RequestParam String yearAndMonth ,@RequestParam String companyId) {
        return generateProdReportService.updateMonthProd(year,yearAndMonth, companyId);
    }

}
